﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using md.imi.membrane.constants;
using md.imi.membranes.objects.rules;
using md.imi.membranes.objects.membranes;

namespace md.imi.membranes.gui.Editor
{
    public partial class TargetMembraneListSelect : Form
    {
        private MembranesTree mSelectedMembrane;
        
        public TargetMembraneListSelect()
        {
            InitializeComponent();
            clInnerMembranes.Items.Clear();
        }

        public MembranesTree GetData()
        {
            return mSelectedMembrane;
        }
        
        public void SetInfoText (String infoText) {
            lblInfo.Text = infoText;    
        }
        
        public void SetInnerMembranesList(List<MembranesTree> innerMembranes, String selectedMembraneLabel) {
            clInnerMembranes.Items.Clear();
            foreach(MembranesTree mt in innerMembranes) {
                
                String membraneLabele = mt.Label != null ? 
                    String.Format("[{0}]", mt.Label) : "{?}";
                
                clInnerMembranes.Items.Add(mt, 
                  !String.IsNullOrEmpty(selectedMembraneLabel) && 
                  selectedMembraneLabel.Equals(mt.Label, StringComparison.CurrentCultureIgnoreCase));
            }
        }
        
        void BtnSaveClick(object sender, System.EventArgs e)
        {
            for (int i = 0; i < clInnerMembranes.Items.Count; i ++ ){
                if (clInnerMembranes.GetItemChecked(i)) {
                    mSelectedMembrane =  (MembranesTree)clInnerMembranes.Items[i];
                    DialogResult = DialogResult.Yes;
                    Close();
                }
            }
        }
        
        
        bool processing = false;
        void ClInnerMembranesItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (processing == true || e.NewValue != CheckState.Checked) {
                return;
            }
            
            processing = true;
            // uncheck other items
            for (int i =0; i <  clInnerMembranes.Items.Count; i ++ ) {
                if (i != e.Index && clInnerMembranes.GetItemChecked(i)) {
                    clInnerMembranes.SetItemChecked(i, false);
                }
            }
            processing = false;
        }
    }
}
